<?php

namespace app\index\controller;

use app\index\model\Gallery;

/**
 * Class Mine
 * @package app\index\controller
 */
class Mine extends Base
{
    /**
     * 我的图库页面
     */
    public function index()
    {
        $galleries = model('gallery')->where('user_id', session('user.id'))->select();
        foreach ($galleries as $gallery) {
            $image = \app\index\model\Image::get($gallery->cover_id);
            $gallery['cover'] = $image->path;
        }
        $this->assign('galleries', $galleries);
        return view();
    }

    /**
     * 新建图库
     */
    public function create()
    {
        $data = getInputData(['name', 'summary']);
        $verify = validateData($data, 'gallery', 'create');
        if ($verify === true) {
            $data['user_id'] = session('user.id');
            $data['status'] = 0;
            $data['cover_id'] = 1;
            model('gallery')->save($data);
            $this->success('创建成功！', 'index');
        } else {
            $this->error('创建失败：' . $verify);
        }
    }

    /**
     * 修改图库页面
     */
    public function edit($id)
    {
        $gallery = Gallery::get($id);
        if (!$gallery) {
            $this->error('图库不存在！');
        }
        if ($gallery->user_id != session('user.id')) {
            $this->error('无权操作此图库！');
        }
        $this->assign('gallery', $gallery);
        return view();
    }

    /**
     * 修改图库
     */
    public function update($id)
    {
        $data = getInputData(['name', 'summary']);
        $gallery = Gallery::get($id);
        if (!$gallery) {
            $this->error('图库不存在！');
        }
        if ($gallery->user_id != session('user.id')) {
            $this->error('无权操作此图库！');
        }
        if (!empty($data['name'])) {
            $gallery->name = $data['name'];
        }
        if (!empty($data['summary'])) {
            $gallery->summary = $data['summary'];
        }
        $gallery->save();
        $this->success('修改成功！', 'index');
    }

    /**
     * 删除图库
     */
    public function delete($id)
    {
        $gallery = Gallery::get($id);
        if (!$gallery) {
            $this->error('图库不存在！');
        }
        if ($gallery->user_id != session('user.id')) {
            $this->error('无权操作此图库！');
        }
        $gallery->delete();
        $images = model('image')->where('gallery_id', $id)->select();
        foreach ($images as $image) {
            $image->delete();
        }
        $this->success('删除成功！', 'index');
    }

    /**
     * 发布或取消发布图库
     */
    public function release($id)
    {
        $gallery = Gallery::get($id);
        if (!$gallery) {
            $this->error('图库不存在！');
        }
        if ($gallery->user_id != session('user.id')) {
            $this->error('无权操作此图库！');
        }
        if ($gallery->status == 0) {
            $gallery->status = 1;
            $msg = '发布成功！';
        } else {
            $gallery->status = 0;
            $msg = '取消发布成功！';
        }
        $gallery->save();
        $this->success($msg, 'index');
    }

    /**
     * 图库详情页面
     */
    public function show($id)
    {
        $gallery = Gallery::get($id);
        if (!$gallery) {
            $this->error('图库不存在！');
        }
        if ($gallery->user_id != session('user.id')) {
            $this->error('无权查看此图库！');
        }
        $this->assign('gallery', $gallery);
        $images = model('image')->where('gallery_id', $id)->select();
        $this->assign('images', $images);
        return view();
    }
}
